package sandau.self_improvement_wall.security;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.servlet.HandlerInterceptor;
import sandau.self_improvement_wall.utils.JWTUtils;
import sandau.self_improvement_wall.vo.BasicMsg;

public class JWTInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response,
                             @NotNull Object handler) {
        BasicMsg basicMsg = new BasicMsg();
        String accessToken = JWTUtils.getAccessToken(request);

        try {
            JWTUtils.verify(accessToken);
            return true;
        } catch (TokenExpiredException e) {

        } catch (SignatureVerificationException e){

        } catch (AlgorithmMismatchException e){

        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(401);
        return false;
    }

}